package com.jtmproject.database;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * This class contains the actions with user's table 
 * @author Javier Tejedor
 */
public class UserDAO implements UserDAOInterface {

	private static UserDAO userDAO;

	/**
	 * this returns an instance of UserDAO. (Singleton Pattern)
	 * @return
	 */
	public static UserDAO getUserDAO() {
		if(userDAO == null){
			userDAO = new UserDAO();
		}
		return userDAO;
	}
	
	/**
	 * returns an object with a specific id
	 */
	public User selectById(int id) {
		Session session = getSession();
		
		User user = (User) session.get(User.class, id);
	    
	    closeSession();
	    return user;
	}


	/**
	 * returns all elements of the table
	 */
	public List<User> selectAll() {
	    Session session = getSession();
	    
		List<User> users = (List<User>) session.createQuery("from User").list();

		closeSession();
	    return users;
	}
	
	/**
	 * returns the elements with the name passed by parameters
	 * @param name
	 * @return
	 */
	public List<User> selectAllByName(String name) {
		Session session = getSession();
		
		List<User> users = null;
		
		Query whereClause = 
				session.createQuery("from User where login = '" + name + "'");
		
		try {
			users = whereClause.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		closeSession();
		return users;
	}
	
	/**
	 * returns the elements with the name passed by parameters
	 * @param name
	 * @return
	 */
	public List<User> selectAllByRol(int rolId) {
		Session session = getSession();
		
		List<User> users = null;
		
		Query whereClause = 
				session.createQuery("from User where roleid = '" + rolId + "'");
		
		try {
			users = whereClause.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		closeSession();
		return users;
	}
	
	
//	/**
//	 * returns the elements with the name passed by parameters
//	 * @param name
//	 * @return
//	 */
//	public List<User> selectAllByRol(String name) {
//		Session session = getSession();
//		
//		List<Role> roles = RoleDAO.getRoleDAO().selectAllByName(name);
//		int size = roles.size();
//		
//		List<User> users = new ArrayList<User>();
//		
//		for(int i = 0; i < size; i++){
//			Query whereClause = 
//					session.createQuery("from User where roleid = '" + roles.get(i).getId() + "'");
//			
//			try {
//				users.addAll(whereClause.list());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		
//		closeSession();
//		return users;
//	}
	
	/**
	 * returns a correct login
	 * @param name
	 * @return
	 */
	public boolean checkLogin(String login, String password) {
		Session session = getSession();
		
		List<User> users = null;
		
		Query whereClause = 
				session.createQuery(
						"from User where login = '" + login + "' AND password = '" + password + "'");
		
		try {
			users = whereClause.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		closeSession();
		return users.size() > 0;
	}

	/**
	 * this inserts one row into the table
	 */
	public void insert(User user) {
		Session session = getSession();
		
	    session.beginTransaction();	    
	    
	    Integer id = (Integer) session.save(user);
	    user.setId(id);
	         
	    session.getTransaction().commit();
	    
	    closeSession();
	}


	/**
	 * this updates one row into the table
	 */
	public void update(User user) {
		Session session = getSession();
		
	    session.beginTransaction();
	    session.merge(user);
	    session.getTransaction().commit();
	    
	    closeSession();
	}

	/**
	 * this deletes one row from the table
	 */
	public void delete(User user) {
		Session session = getSession();
		
	    session.beginTransaction();
	    session.delete(user);
	    session.getTransaction().commit();
	    
	    closeSession();
	}

	/**
	 * This method saves or updates, depending if the object exist or not
	 * @param user
	 */
	public void saveOrUpdate(User user) {
		Session session = getSession();
		
	    session.beginTransaction();
	    
	    session.saveOrUpdate(user);
	    session.getTransaction().commit();
	    
	    closeSession();
	}

	/**
	 * this returns the session
	 * @return
	 */
	private Session getSession(){
		return HibernateSession.getSession();
	}
	
	/**
	 * this close the session
	 */
	private void closeSession(){
		HibernateSession.closeSession();
	}

}
